﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Kimd.Motion
{
    public class SingleAxisMoveStep : Step
    {
        private string _actualPoint;
        public string AxisName { get; set; }
        public string PointName { get; set; }
        public SingleAxisMoveStep()
        {

        }
        public SingleAxisMoveStep(string description, StepType stepType)
            : base(description, stepType)
        {

        }
        public override void Prepare(Dictionary<string, Axis> machineAxis, Dictionary<string, Cylinder> machineCylinder, Tray tray = null, int trayIndex = -1, int nozzleIndex = -1)
        {
            if (machineAxis[AxisName].MotionPosition.ContainsKey(PointName))
                _actualPoint = PointName;
            else
                _actualPoint = $"{PointName}-{trayIndex}-{nozzleIndex}";
            machineAxis[AxisName].SetSpeed(SpeedType.Manual);
            base.Prepare(machineAxis, machineCylinder);
        }
        public override bool Run(Dictionary<string, Axis> machineAxis, Dictionary<string, Cylinder> machineCylinder, Dictionary<string, Sensor> machineSensor, out string errInfo)
        {
            machineAxis[AxisName].Move2Point(_actualPoint, out errInfo, 0);
            if (!string.IsNullOrEmpty(errInfo))
                return false;
            return true;
        }
        public override TreeNode ToTreeNode()
        {
            TreeNode node = new TreeNode();
            node.Text = this.ToString();
            node.Tag = this;
            return node;
        }
        public override void Stop(Dictionary<string, Axis> machineAxis)
        {
            machineAxis[AxisName].StopMovement = true;
        }
        public override string ToString()
        {
            return $"{this.Description}-[{AxisName}移动至{PointName}]";
        }
    }
}
